# from PyPDF2 import PdfReader, PdfWriter
# from pathlib import Path
# import time
    
# def pdf_write(func):
#     def wrapper(*args, **kwargs):
#         result = func(*args, **kwargs)
#         print(kwargs)
#         out_pdf_path = kwargs['out_pdf_path']
#         current_time = time.strftime("%Y_%m_%d--%H_%M_%S", time.localtime())
#         with open(f'{out_pdf_path}/处理后{current_time}.pdf', 'wb') as out:
#             result.write(out)
#     return wrapper

        
# #提取pdf页面
# @pdf_write
# def split_pdf_Page(start_pageNumber,end_pageNumber,*,pdf_path,out_pdf_path):
#     pdf_reader = PdfReader(pdf_path)
#     pdf_writer = PdfWriter()
    
#     pdf_pagesNumber = len(pdf_reader.pages)
#     if end_pageNumber > pdf_pagesNumber or start_pageNumber > pdf_pagesNumber : return
#     for page in range(start_pageNumber,end_pageNumber):
#         pdf_writer.add_page(pdf_reader.pages[page])
        
#     return pdf_writer

        
# pdf_path = Path(r"./abc.pdf")    
# split_pdf_Page(1,3,pdf_path=pdf_path,out_pdf_path='.')

# @pdf_write
# def merge_pdf_Page(*,pdf_path_list,out_pdf_path):
#     pdf_writer = PdfWriter()
#     for pdf_path in pdf_path_list:
#         pdf_reader = PdfReader(pdf_path)
#         pdf_pagesNumber = len(pdf_reader.pages)
#         for page in range(pdf_pagesNumber):
#             pdf_writer.add_page(pdf_reader.pages[page])
            
#     return pdf_writer

# pdf_path_list = [Path(r"./处理后23_34_30.pdf"),Path(r"./处理后23_34_46.pdf")]    


# if __name__ == '__main__':
#     split_pdf_Page(1,3,pdf_path=pdf_path,out_pdf_path='.')
#     merge_pdf_Page(pdf_path_list=pdf_path_list,out_pdf_path='.')

from PyPDF2 import PdfReader, PdfWriter
from pathlib import Path
import os
import time
import tkinter as tk
from tkinter import filedialog
import subprocess

def pdf_write(func):
    def wrapper(*args, **kwargs):
        result = func(*args, **kwargs)
        print(kwargs)
        out_pdf_path = kwargs['out_pdf_path']
        current_time = time.strftime("%Y_%m_%d--%H_%M_%S", time.localtime())
        out_filename = f'{out_pdf_path}/处理后{current_time}.pdf'.replace('/', '\\')
        with open(out_filename, 'wb') as out:
            result.write(out)
        print(out_filename)
        file_dir = os.path.dirname(out_filename) 
        subprocess.Popen(['explorer', '/select,', out_filename], cwd=file_dir)
        subprocess.Popen(['start', '', out_filename], shell=True)
        # 添加一个弹窗提示
        tk.messagebox.showinfo('完成', 'PDF处理完成')
        
    return wrapper

@pdf_write
def split_pdf_Page(start_pageNumber, end_pageNumber, *, pdf_path, out_pdf_path):
    pdf_reader = PdfReader(pdf_path)
    pdf_writer = PdfWriter()

    pdf_pagesNumber = len(pdf_reader.pages)
    if end_pageNumber > pdf_pagesNumber or start_pageNumber > pdf_pagesNumber:
        return
    for page in range(start_pageNumber, end_pageNumber):
        pdf_writer.add_page(pdf_reader.pages[page])

    return pdf_writer

@pdf_write
def merge_pdf_Page(*, pdf_path_list, out_pdf_path):
    pdf_writer = PdfWriter()
    for pdf_path in pdf_path_list:
        pdf_reader = PdfReader(pdf_path)
        pdf_pagesNumber = len(pdf_reader.pages)
        for page in range(pdf_pagesNumber):
            pdf_writer.add_page(pdf_reader.pages[page])

    return pdf_writer


def select_pdf_file():
    # 使用文件选择对话框选择PDF文件
    file_path = filedialog.askopenfilename(filetypes=[('PDF Files', '*.pdf')])
    pdf_path_entry.delete(0, tk.END)
    pdf_path_entry.insert(tk.END, file_path)


def select_output_directory():
    # 使用文件夹选择对话框选择输出目录
    output_dir = filedialog.askdirectory()
    out_pdf_path_entry.delete(0, tk.END)
    out_pdf_path_entry.insert(tk.END, output_dir)
    
def select_pdf_files():
    # 使用文件选择对话框选择多个PDF文件
    file_paths = filedialog.askopenfilenames(filetypes=[('PDF Files', '*.pdf')])
    pdf_paths_entry.delete(0, tk.END)
    pdf_paths_entry.insert(tk.END, ';'.join(file_paths))
    
def select_merge_output_directory():
    # 使用文件夹选择对话框选择输出目录
    output_dir = filedialog.askdirectory()
    merge_out_pdf_path_entry.delete(0, tk.END)
    merge_out_pdf_path_entry.insert(tk.END, output_dir)

def split_pdf():
    start_page = int(start_page_entry.get())
    end_page = int(end_page_entry.get())
    pdf_path = pdf_path_entry.get()
    out_pdf_path = out_pdf_path_entry.get()
    split_pdf_Page(start_page, end_page, pdf_path=pdf_path, out_pdf_path=out_pdf_path)


def merge_pdf():
    pdf_paths = pdf_paths_entry.get().split(';')
    out_pdf_path = merge_out_pdf_path_entry.get()
    pdf_path_list = [Path(path) for path in pdf_paths]
    merge_pdf_Page(pdf_path_list=pdf_path_list, out_pdf_path=out_pdf_path)


# 创建一个简单的GUI窗口
window = tk.Tk()
window.title("PDF合并与拆分小工具")

# 设置窗口宽高，并使窗口页面居中
window.geometry("400x300")
window.eval('tk::PlaceWindow %s center' % window.winfo_toplevel())

# 添加控件
# 添加控件
start_page_label = tk.Label(window, text="起始页码:")
start_page_label.grid(row=0, column=0)

start_page_entry = tk.Entry(window)
start_page_entry.grid(row=0, column=1)

end_page_label = tk.Label(window, text="结束页码:")
end_page_label.grid(row=1, column=0)

end_page_entry = tk.Entry(window)
end_page_entry.grid(row=1, column=1)

pdf_path_label = tk.Label(window, text="PDF文件路径:")
pdf_path_label.grid(row=2, column=0)

pdf_path_entry = tk.Entry(window)
pdf_path_entry.grid(row=2, column=1)

pdf_path_button = tk.Button(window, text="选择文件", command=select_pdf_file)
pdf_path_button.grid(row=2, column=2)

out_pdf_path_label = tk.Label(window, text="输出目录:")
out_pdf_path_label.grid(row=3, column=0)

out_pdf_path_entry = tk.Entry(window)
out_pdf_path_entry.grid(row=3, column=1)

out_pdf_path_button = tk.Button(window, text="选择目录", command=select_output_directory)
out_pdf_path_button.grid(row=3, column=2)

split_pdf_button = tk.Button(window, text="提取PDF页面", command=split_pdf)
split_pdf_button.grid(row=4, column=1, columnspan=2, sticky='w')

pdf_paths_label = tk.Label(window, text="要合并的PDF文件路径:")
pdf_paths_label.grid(row=5, column=0)

pdf_paths_button = tk.Button(window, text="选择文件", command=select_pdf_files)
pdf_paths_button.grid(row=5, column=2)

pdf_paths_entry = tk.Entry(window)
pdf_paths_entry.grid(row=5, column=1)

merge_out_pdf_path_label = tk.Label(window, text="输出目录:")
merge_out_pdf_path_label.grid(row=6, column=0)

merge_out_pdf_path_entry = tk.Entry(window)
merge_out_pdf_path_entry.grid(row=6, column=1)

merge_out_pdf_path_button = tk.Button(window, text="选择目录", command=select_merge_output_directory)
merge_out_pdf_path_button.grid(row=6, column=2)

merge_pdf_button = tk.Button(window, text="合并PDF页面", command=merge_pdf)
merge_pdf_button.grid(row=7, column=1, columnspan=2, sticky='w')


if __name__ == '__main__':
    # 启动主事件循环
    window.mainloop()
